package com.wsj.a.collapsingdemo.ui;

import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.wsj.a.collapsingdemo.R;
import com.wsj.a.collapsingdemo.databinding.ActivityTestRefreshBinding;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

public class TestRefreshActivity extends AppCompatActivity {

    private ActivityTestRefreshBinding mBinding;
    private String[] infos= new String[]{"今天是什么日子","今天是农历七月十五呀","那今天也是节日哟,你要回家过节吗?","我也想,但是不放假呀..."};
    private String selectedInfo ="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(TestRefreshActivity.this,R.layout.activity_test_refresh);
        initView();
        autoRefresh();
    }

    public void initView(){
        mBinding.storeHousePtrFrame.setPtrHandler(new PtrHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                mBinding.storeHousePtrFrame.refreshComplete();

                if(TextUtils.isEmpty(selectedInfo)){
                     selectedInfo = infos[0];
                 }else if(selectedInfo.equals(infos[0])){
                     selectedInfo = infos[1];
                 }else if(selectedInfo.equals(infos[1])){
                     selectedInfo = infos[2];
                 }else if(selectedInfo.equals(infos[2])){
                     selectedInfo = infos[3];
                 }else if(selectedInfo.equals(infos[3])){
                     selectedInfo= infos[0];
                 }

                 mBinding.selectText.setText(selectedInfo);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }
        });
    }

    public void autoRefresh(){
        mBinding.storeHousePtrFrame.postDelayed(new Runnable() {
            @Override
            public void run() {
                mBinding.storeHousePtrFrame.autoRefresh();
            }
        }, 100);

    }
}
